package com.zhengb.algorithmdemo.basic.tree;

import java.util.List;

import static com.zhengb.algorithmdemo.basic.tree.BinaryTreeTraverse.*;

/**
 * Created by zhengbo on 2020/2/20.
 */
public class BinaryTreeTest {

    public static void main(String[] args) {

        BinaryTreeNode<Integer> rootNode = new BinaryTreeNode<>(1);
        BinaryTreeNode<Integer> node2 = new BinaryTreeNode<>(2);
        BinaryTreeNode<Integer> node3 = new BinaryTreeNode<>(3);
        BinaryTreeNode<Integer> node4 = new BinaryTreeNode<>(4);
        BinaryTreeNode<Integer> node5 = new BinaryTreeNode<>(5);
        BinaryTreeNode<Integer> node6 = new BinaryTreeNode<>(6);
        BinaryTreeNode<Integer> node7 = new BinaryTreeNode<>(7);


        rootNode.setLeftNode(node2);
        rootNode.setRightNode(node3);

        node2.setLeftNode(node4);
        node2.setRightNode(node5);

        node3.setLeftNode(node6);
        node3.setRightNode(node7);

        System.out.println("前序遍历...");
        preOrderTraverse(rootNode);
        System.out.println("非递归前序遍历...");
        preOrderNoRecursive(rootNode);

        System.out.println("中序遍历...");
        inOderTraverse(rootNode);

        System.out.println("后序遍历...");
        postOderTraverse(rootNode);

        List<Integer> traverseTreeWithPreOrder = traverseTreeWithPreOrder(rootNode);
        System.out.println(traverseTreeWithPreOrder);

        List<Integer> traverseTreeWithInOrder = traverseTreeWithInOrder(rootNode);
        System.out.println(traverseTreeWithInOrder);

        List<Integer> traverseTreeWithPostOrder = traverseTreeWithPostOrder(rootNode);
        System.out.println(traverseTreeWithPostOrder);

        System.out.println("层次遍历树....");
        levelOrder(rootNode);

        int maxData = BinaryTreeUtils.findMaxData(rootNode);

        System.out.println("递归查找二叉树最大值：" + maxData);

        maxData = BinaryTreeUtils.findMaxUsingLevelOrder(rootNode);

        System.out.println("非递归查找二叉树最大值：" + maxData);

        BinaryTreeNode<Integer> elementInBinaryNode = BinaryTreeUtils.findElementInBinaryNode(rootNode, 2);

        System.out.println("递归二叉树查找元素：" + elementInBinaryNode);

        elementInBinaryNode = BinaryTreeUtils.findElementInBinaryNodeUseLevelOrder(rootNode, 2);

        System.out.println("非递归二叉树查找元素：" + elementInBinaryNode);

        System.out.println("插入一个新节点...");
        BinaryTreeUtils.insertInBinaryTreeNode(rootNode, 10);
        preOrderTraverse(rootNode);

        System.out.println("二叉树节点个数:" + BinaryTreeUtils.sieOfBinaryTree(rootNode));
        System.out.println("二叉树节点个数:" + BinaryTreeUtils.sieOfBinaryTreeUsingLevelOrder(rootNode));

        System.out.println("逆向遍历树的元素：" + BinaryTreeUtils.levelOrderTraverseInReverse(rootNode));
        System.out.println("递归计算树的高度：" + BinaryTreeUtils.heightOfBinaryTree(rootNode));
        System.out.println("非递归计算树的高度：" + BinaryTreeUtils.heightOfBinaryTreeNoRecursive(rootNode));

    }
}
